var rolesinfoManager;
var rolesinfoItemPageOpen;
var rolesinfoItem;

function initRolesInfoManager(){
	var colimnsArray = [];
	
	colimnsArray.push({
			display : '编号',
			name : 'id',
			align : 'left',
			showTitle : true,
			minWidth : 100,
			align : "center",
			editor: { type: 'text'},
			isSort : false
		});
	colimnsArray.push({
		display : '角色名称',
		name : 'name',
		align : 'left',
		showTitle : true,
		minWidth : 100,
		align : "center",
		editor: { type: 'text'},
		isSort : false
	});
	colimnsArray.push({
		display : '操作',
		align : 'left',
		showTitle : false,
		minWidth : 100,
		align : "center",
		render : function(rowdata, rowindex) {
            return " <a  href='#' onClick='openRolesInfoGrantPage("+rowdata.id+",\""+rowdata.name+"\");'>授权</a>  |  <a  href='#' onClick='alert(1);'>删除</a>";
		}
	});

	var menu = $.ligerMenu({ width: 120, items:
		[
			{ text: '增加', click: function(){openRolesInfoItemPage('create',"新增")}, icon: 'add' },
			{ line: true },
			{ text: '修改', click: function(){openRolesInfoItemPage('edit',"编辑")} },
			{ line: true },
			{ text: '查看', click: function(){openRolesInfoItemPage('show',"查看")} }
		]
	}); 
	
	return window['g'] = jQuery("#maingrid").ligerGrid({
		height : '100%',
		url:'/admin/security/rolesInfo/ajaxList.do',
		columns : colimnsArray,
		pageSize : 10,
		rownumbers : true,
		toolbar : {
			items : [ {
				text : '保存',
				click : saveUpdateRolesInfoManager,
				icon : 'modify'
			}, {
				line : true
			}, {
				text : '增加',
				click : function(){openRolesInfoItemPage('create',"新增")},
				icon : 'add'
			}, {
				line : true
			}]
		},
		onContextmenu : function (parm,e)
        {
			rolesinfoItem = parm.data;
	        menu.show({ top: e.pageY, left: e.pageX });
	        return false;
        },
        enabledEdit: false, 
        isScroll: false, 
        rownumbers:true
	});
};

var rolesInfoGrantManger;
function doSaveRoleActionsInfos(newData,roleIdVal){
	jQuery.ajax({
		url:'/admin/security/roleActionsInfo/saveOrUpdate.do',
		data:{json:JSON.stringify(newData),roleId:roleIdVal},
		dataType:'json',
		type:'post',
		success:function(data){
			if(data.sucessSize > 0){
				$.ligerDialog.success('恭喜,已经保存成功!');
				tip = $.ligerDialog.tip({ title: '提示信息', content: '恭喜,已经保存'+data.sucessSize+'条记录!' });
				rolesinfoItemPageOpen.hidden();
			}else{
				if(data.msg){
					tip = $.ligerDialog.tip({ title: '提示信息', content: data.msg });
				}else{
					tip = $.ligerDialog.tip({ title: '提示信息', content: '网络异常,没有保存成功!' });
				}
			}
		},
		error:function(){
			$.ligerDialog.error("网络发生异常,请稍后再试!");
		}
	});
};
function openRolesInfoGrantPage(itemId,itemName){
	jQuery.ajax({
		url:'/admin/security/roleActionsInfo/ajaxList.do',
		data:{roleId:itemId},
		dataType:'json',
		type:'post',
		success:function(data){
			if(data){
				for(var i in data.actions){
					for(var j in data.ras.Rows){
						if(data.actions[i].id == data.ras.Rows[j].actionId){
							data.actions[i].ischecked = true;
						}
					}
				}
				if(rolesInfoGrantManger){
					rolesInfoGrantManger.clear();
				}
				rolesInfoGrantManger = jQuery('#grantDiv').ligerTree({
					data:data.actions,
					idFieldName :'id',
					textFieldName : 'name',
					parentIDFieldName :'pid'
				});
				rolesinfoItemPageOpen = $.ligerDialog.open({ target: $("#grantDivWrapper"),isResize:true,title:'授权-'+itemName,width:450 });
				jQuery('#grantRolesInfoSave').unbind('click').click(function(){
					var notes = rolesInfoGrantManger.getChecked();
					var checkList = [];
					for (var i in notes){
						var temp = {};
						temp.roleId = itemId;
						temp.actionId = notes[i].data.id;
						checkList.push(temp);
					}
					jQuery.ligerDialog.confirm('确定要进行授权吗?',function (yes,value) {
						if(yes){
							doSaveRoleActionsInfos(checkList,itemId);
						}
						});
				});
				jQuery('#grantRolesInfoReset').unbind('click').click(function(){
					
				});
			}else{
				$.ligerDialog.error("无法读取数据,请稍后再试!");
			}
		},
		error:function(){
			$.ligerDialog.error("网络发生异常,请稍后再试!");
		}
	});
}

function saveUpdateRolesInfoManager(item){
	var data = rolesinfoManager.getUpdated();
	if(data.length == 0){
		$.ligerDialog.warn("您还未做任何修改哦!");
		return;
	}
	doSaveRolesInfos(data);
};
function doSaveRolesInfos(newData,add){
	jQuery.ajax({
		url:'/admin/security/rolesInfo/saveOrUpdate.do',
		data:{json:JSON.stringify(newData)},
		dataType:'json',
		type:'post',
		success:function(data){
			if(data.sucessList.length > 0){
				$.ligerDialog.success('恭喜,已经保存成功!');
				tip = $.ligerDialog.tip({ title: '提示信息', content: '恭喜,已经保存'+data.sucessList.length+'条记录!' });
				rolesinfoManager.loadServerData({pagesize:10});
				rolesinfoItemPageOpen.hidden();
			}else{
				if(data.msg){
					tip = $.ligerDialog.tip({ title: '提示信息', content: data.msg });
				}else{
					tip = $.ligerDialog.tip({ title: '提示信息', content: '网络异常,没有保存成功!' });
				}
			}
		},
		error:function(){
			$.ligerDialog.error("网络发生异常,请稍后再试!");
		}
	});
};
/**
 * 
 * @param item
 * @param target
 */
function openRolesInfoItemPage(target,titleName){
	jQuery.ajax({
		url:'/admin/security/rolesInfo/'+target+'.do',
		data:{id:(rolesinfoItem?rolesinfoItem.id:0)},
		dataType:'text',
		type:'post',
		success:function(data){
			if(data){
				jQuery("#openDiv").html(data);
				rolesinfoItemPageOpen = $.ligerDialog.open({ target: $("#openDiv"),isResize:true,title:titleName,width:450 });
			}
		},
		error:function(){
			$.ligerDialog.error("网络发生异常,请稍后再试!");
		}
	});
};

jQuery(function() {
	rolesinfoManager = initRolesInfoManager();
    jQuery("#pageloading").hide();
});